using WCell.RealmServer.Entities;
using WCell.RealmServer.GameObjects;
using WCell.RealmServer.GameObjects.GOEntries;
using WCell.Constants.World;
using WCell.RealmServer.Global;
using System;
using WCell.Util.Data;

namespace WCell.RealmServer.GameObjects
{
	/// <summary>
	/// Walk-in portal
	/// </summary>
	public class GOPortalEntry : GOCustomAreaEffectEntry
	{
		public static int UpdateDelayMillis = 2000;

		public GOPortalEntry()
		{
			Id = 50001;
			DisplayId = 4396;
			AreaEffectHandler = TeleportUnit;
			UseHandler = OnUse;
			GOCreator = () => new Portal();
			
			// TODO: Fix!
			UpdateTicks = UpdateDelayMillis/Region.DefaultUpdateDelay;
		}

		private void TeleportUnit(GameObject go, Character chr)
		{
			if (go.Handler.CanBeUsedBy(chr))
			{
				// We need to enqueue a message when moving/removing/adding Objects in this method
				var portal = (Portal)go;
				chr.AddMessage(() => chr.TeleportTo(portal.Target));
			}
		}

		private bool OnUse(GameObject go, Character chr)
		{
			var portal = (Portal)go;
			chr.TeleportTo(portal);
			return true;
		}
	}
}